<?php
namespace  Android\Model;
use Think\Model;

class ActivityModel extends Model
{
    protected function _before_insert(&$data, $option)
    {

//        $upload = new \Think\Upload();// 实例化上传类
//        $upload->maxSize = 3145728;// 设置附件上传大小
//        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
//        $upload->rootPath = './public/Uploads/'; // 设置附件上传根目录
//        $upload->savePath = ''; // 设置附件上传（子）目录
//        // 上传文件
//        $info = $upload->upload();
//        if (!$info) {// 上传错误提示错误信息
//            $this->error($upload->getError());
//        } else {// 上传成功
//            $this->success('上传成功！');
//        }

    }
    //把图片保存到数据库
    // 数据添加到数据库中之后,$data['id']就是新添加的记录的ID
    protected function _after_insert($data, $option)
    {
        /**************************** 处理商品相册 **************************************/
        if (hasImage('pic')) {
            // 重新处理数组
            foreach ($_FILES['pic']['name'] as $k => $v) {
                if (empty($v))
                    continue;
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error'][$k],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
            $_FILES = $pics;  // uploadOne函数中会到$_FILES数组中找图片，所以把处理好的图片信息传到这里
            // 循环这个数组然后一个一个上传并生成缩略图
            $gpModel = D('act_images');
            foreach ($pics as $k => $v) {
                $ret = uploadOne($k, 'Goods');
                $sub=$ret['images'][0] ;
              $str=  substr(strrchr($sub, '.'), 1);
                if(in_array("$str",array('mp4','rmvb','mkv','avi' ))){
                    $gpModel->add(array(
                        'act_id' => $data['act_id'],
                        'video' => $ret['images'][0],)
                    );
                }else{
                    $gpModel->add(array(
                        'act_id' => $data['act_id'],
                        'image' => $ret['images'][0],
                    ));
                }


            }
        }

    }



















    //获取拼单/活动的详情/列表页
    /*
     * @param int$actid  活动的id
     *      int $user_type   用户的类型 （1用户2商家）
     *     int $type  活动的类型1是活动2是拼单
     *    int $status 状态未开始1 正在进行2 已经结束3
     *   @return
     */
    public function Act_together(&$actid,&$user_type,&$type,$status=""){
        $str= date('y-m-d h:i:s',time());
        $status="1=1";
        switch($status){
            case 1:
                $status=" and gene_time>'".$str."'";
                break;
            case 2:
                $status=" and gene_time>'".$str."' and '".$str."' < end_time";
                break;
            case 3:
                $status=" and '".$str."'< end_time";
                break;
        }
        //查询列表页面
             //查询的是一个
             $info=$this->field('gene_time,place,text,account_type')->where('act_id='.$actid." and user_type=".$user_type." and type=".$type. $status)->find();
             //echo $this->getLastSql();
             if($type==2){
                 $info['intro']=$this->alias(ac)->join('shop sh on ac.shop_id=sh.shop_id')->where('ac.act_id='.$actid." and ac.user_type=".$user_type." and ac.type=".$type)->field('sh.intro')->find();

                 $info['seller']=$this->alias(ac)->join('seller se on se.seller_id=ac.user_id ')->field('user_name,head_img')->where('ac.act_id='.$actid." and ac.user_type=".$user_type)->find();
             }
             $join_act=M(join_act);
             $user_id=$join_act->field('user_id')->where('act_id='.$actid)->select();
             $user= M('user');
             foreach ($user_id as $k=>$v){
                 $info['join_user'][]=$user->field('nickname,head_img')->where("user_id=".$v['user_id'])->find();
             }
        return $info;
    }
   //查询列表页面
    /*
   * @param int$actid  活动的id
   *      int $user_type   用户的类型 （1用户2商家）
   *     int $type  活动的类型1是活动2是拼单
   *    int $status 状态未开始1 正在进行2 已经结束3
   *   @return
   */
    public function Act_togetherlist(&$status="",&$actids){
        $str= date('y-m-d h:i:s',time());
        $star="1=1 and ";
        switch($status){
            case 1:
                $status=$star." and gene_time>'".$str."'";
                break;
            case 2:
                $status=$star." and gene_time>'".$str."' and '".$str."' < end_time";
                break;
            case 3:
                $status=$star." and '".$str."'< end_time";
                break;
        }

        //查询列表页面
        if($status){
            $actid=$this->field('act_id')->where($status)->select();
        }else {
            $actid = $actids;

        }
        $join_act=M(join_act);
        $info['act'] ='';
        $info['pindan']='';
        foreach ($actid as $key=>$v){
            //查询的是一个
            $actid=$v['act_id'];
            $type = $this->field('type')->where('act_id='.$actid)->find();
            $types=$type['type'];
            if($types==2){
                $info['pindan'][$key]=$this->field('act_id,shop_id,gene_time,place,text,account_type')->where('act_id='.$actid." and user_type=2 and type=2 ". $status)->find();
                $info['pindan'][$key]['intro']=$this->alias(ac)->join('shop sh on ac.shop_id=sh.shop_id')->where('ac.act_id='.$actid." and ac.user_type=2 and ac.type= 2")->field('sh.intro')->find();

                $info['pindan'][$key]['seller']=$this->alias(ac)->join('seller se on se.seller_id=ac.user_id ')->field('user_name,head_img')->where('ac.act_id='.$actid." and ac.user_type=2")->find();
                $join_act=M(join_act);
                $user_id=$join_act->field('user_id')->where('act_id='.$actid)->select();
                $user= M('user');
                foreach ($user_id as $k=>$v){
                    $info['pindan'][$key]['join_user'][]=$user->field('user_id,nickname,head_img')->where("user_id=".$v['user_id'])->find();
                }
            }
            if($types==1){

                $info['act'][]=$this->field('act_id,budget,gene_time,place,text,account_type')->where('act_id='.$actid." and user_type=1 and type=1 ". $status)->order('time1 desc')->find();
                $user_id=$join_act->field('user_id')->where('act_id='.$actid)->select();
                $user= M('user');
                foreach ($user_id as $k=>$v){
                    $info['act'][$key]['join_user'][]=$user->field('user_id,nickname,head_img')->where("user_id=".$v['user_id'])->find();
                }
            }
        }
        return $info;
    }
    //获取热门推荐的
    public function  hot(){
       $info= $this->field('theme,act_id')->where('hot=1')->select();
        return $info;
    }
    //查询参加和发布的活动
    public function Myact(&$user_id){
        $join_act= D('join_act');
        $activity=  M('activity');
        $join=  $join_act->field('act_id')->where('user_id='.$user_id)->select();
        $pub=$activity->field('act_id')->where('user_id='.$user_id." and user_type=1")->select();
        $act=array_merge($join,$pub);
        $info=$this->Act_togetherlist($status='',$act);
        return $info;
    }

}

